package com.recruit.server.service.impl;

import com.recruit.server.common.HeaderInfo;
import com.recruit.server.dao.CategoryMapper;
import com.recruit.server.entity.pojo.Category;
import com.recruit.server.request.category.AddCategoryRequest;
import com.recruit.server.request.category.ListCategoryRequest;
import com.recruit.server.request.category.UpdateCategoryRequest;
import com.recruit.server.response.category.AddCategoryResponse;
import com.recruit.server.response.category.ListCategoryResponse;
import com.recruit.server.response.category.UpdateCategoryResponse;
import com.recruit.server.response.shipping.ListShippingResponse;
import com.recruit.server.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ListCategoryResponse list(ListCategoryRequest request, HeaderInfo headerInfo) {
        ListCategoryResponse response = new ListCategoryResponse();
        try {
            List<Category> categoryList = categoryMapper.selectAllCategorys();
            response.setCategoryList(categoryList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    @Override
    public AddCategoryResponse add(AddCategoryRequest request, HeaderInfo headerInfo) {
        AddCategoryResponse response = new AddCategoryResponse();
        try {
            Category category = new Category();
            category.setName(request.getCategoryName());
            category.setParentId(request.getParentId());
            category.setStatus(true);
            categoryMapper.insert(category);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    @Override
    public UpdateCategoryResponse update(UpdateCategoryRequest request, HeaderInfo headerInfo) {
        UpdateCategoryResponse response = new UpdateCategoryResponse();
        try {
            Category category = new Category();
            category.setId(request.getCategoryId());
            category.setName(request.getCategoryName());
            category.setParentId(request.getParentId());
            category.setStatus(request.getStatus());
            categoryMapper.updateByPrimaryKeySelective(category);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }
}
